package com.ems.sys.controller;
import com.ems.entity.Result;
import com.ems.entity.StatusCode;
import com.ems.sys.pojo.Department;
import com.ems.sys.service.DepartmentService;
import com.github.pagehelper.PageInfo;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/****
 * @Author:传智播客
 * @Description:
 * @Date 2019/6/14 0:18
 *****/

@RestController
@RequestMapping("/sys/department")
//@CrossOrigin
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /***
     * Department分页条件搜索实现
     * @param department
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false)  Department department, @PathVariable  int page, @PathVariable  int size){
        //调用DepartmentService实现分页条件查询Department
        PageInfo<Department> pageInfo = departmentService.findPage(department, page, size);
        return new Result(true, StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * Department分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用DepartmentService实现分页查询Department
        PageInfo<Department> pageInfo = departmentService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param department
     * @return
     */
    @PostMapping(value = "/search" )
    public Result<List<Department>> findList(@RequestBody(required = false)  Department department){
        //调用DepartmentService实现条件查询Department
        List<Department> list = departmentService.findList(department);
        return new Result<List<Department>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        //调用DepartmentService实现根据主键删除
        departmentService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改Department数据
     * @param department
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody  Department department,@PathVariable String id){
        //设置主键值
        department.setdNum(id);
        //调用DepartmentService实现修改Department
        departmentService.update(department);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Department数据
     * @param department
     * @return
     */
    @PostMapping
    public Result add(@RequestBody   Department department){
        //调用DepartmentService实现添加Department
        departmentService.add(department);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询Department数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Department> findById(@PathVariable String id){
        //调用DepartmentService实现根据主键查询Department
        Department department = departmentService.findById(id);
        return new Result<Department>(true,StatusCode.OK,"查询成功",department);
    }

    /***
     * 查询Department全部数据
     * @return
     */
    @GetMapping
    public Result<List<Department>> findAll(){
        //调用DepartmentService实现查询所有Department
        List<Department> list = departmentService.findAll();
        return new Result<List<Department>>(true, StatusCode.OK,"查询成功",list) ;
    }
}
